package editor;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.awt.event.WindowListener;
import java.awt.event.WindowStateListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneLayout;
import javax.swing.UIManager;

import editor.Menu.ZOOM;

public class Editor extends JFrame implements WindowListener,
WindowFocusListener,
WindowStateListener{
	private static final long serialVersionUID = 1L;
	private GridPanel grid;
	private JScrollPane scrollPane;
	private static Toolkit kit = Toolkit.getDefaultToolkit();
	private static final int WIDTH = (int) kit.getScreenSize().getWidth();
	private static final int HEIGHT= (int) kit.getScreenSize().getHeight() - 100;
	public  static int 	cellW;
	public  static int 	cellH;
	private Menu 		menu;
	public static boolean isDirty;
	
	public Editor()
	{
		super();
		new Grid();
		new JPanel();
		
		addWindowListener(this);
        addWindowFocusListener(this);
        addWindowStateListener(this);
		
		grid = new GridPanel();
		isDirty=false;
		
		int d = Math.min(WIDTH, HEIGHT);
		Dimension dim = new Dimension(d, d);
		System.out.println("DIMENSIONI: " + dim.toString());
        
		Image img = kit.getImage("src/iconMenu/3design.png");
		menu = new Menu(this);
		this.setTitle("TRACE-ON MAP EDITOR");
		this.setLayout(new BorderLayout());
		this.setIconImage(img);
		this.setSize(new Dimension(WIDTH, HEIGHT));
		this.setVisible(true);
		this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		//this.setLayout(boxLayout);
		JFrame.setDefaultLookAndFeelDecorated(true);
		
		grid.setSize(dim);
		grid.setPreferredSize(dim);
		grid.setMinimumSize(dim);
		grid.setMaximumSize(new Dimension(d*8, d*8));
		System.out.println("SIZE GRID BEFORE: " + grid.getSize().toString());
		
		scrollPane=new JScrollPane(grid);
		scrollPane.setLayout(new ScrollPaneLayout());
		scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); 
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scrollPane.setAutoscrolls(true);
		scrollPane.setViewportView(grid);
		scrollPane.setVisible(true);
		scrollPane.setSize(dim);
		scrollPane.setPreferredSize(dim);
		scrollPane.setMinimumSize(new Dimension(0, 0));
		scrollPane.setMaximumSize(dim);
		
		menu.setVisible(true);
		
		cellW = (int) (this.getSize().getWidth()/Grid.WIDTH);
		cellH = (int) (this.getSize().getHeight()/Grid.HEIGHT);
		
		this.setJMenuBar(menu);
		this.add(scrollPane);
		this.setZoom(ZOOM.REALSIZE);
		
		System.out.println("RATIO ("+cellW+","+cellH+")");
		System.out.println("SCROLLPANE SIZE ("+ scrollPane.getWidth() + "," + scrollPane.getHeight()+")");
		System.out.println("SCROLLPANE POINT ("+ scrollPane.getLocation().x + "," + scrollPane.getLocation().y+")");
		System.out.println("GRID SIZE ("+ grid.getWidth() + "," + grid.getHeight()+")");
		System.out.println("GRID POINT ("+ grid.getLocation().x + "," + grid.getLocation().y+")");
	}
	
	public static void main(String[] args) {
		try {
	        //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
	        UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
	    } catch (Exception e) { }

		Editor editor=new Editor();
		System.out.println(editor.getTitle());
	}
	
	public void setZoom(ZOOM z)
	{
		System.out.println("ZOOM BY " + z);
		switch(z)
		{
		case OCTSIZE: 
			cellW = (int) (this.getSize().getWidth()/Grid.WIDTH);
			cellH = (int) (this.getSize().getHeight()/Grid.HEIGHT);
			cellW = (int) Math.min(cellW, cellH)*8;
			cellH = (int) Math.min(cellW, cellH)*8;
			break;
		case QUADSIZE: 
			cellW = (int) (this.getSize().getWidth()/Grid.WIDTH);
			cellH = (int) (this.getSize().getHeight()/Grid.HEIGHT);
			cellW = Math.min(cellW, cellH)*4;
			cellH = Math.min(cellW, cellH)*4;
			break;
		case DOUBLESIZE: 
			cellW = (int) (this.getSize().getWidth()/Grid.WIDTH);
			cellH = (int) (this.getSize().getHeight()/Grid.HEIGHT);
			cellW = Math.min(cellW, cellH)*2;
			cellH = Math.min(cellW, cellH)*2;
			break;
		case REALSIZE: 
			cellW = (int) (this.getSize().getWidth()/Grid.WIDTH);
			cellH = (int) (this.getSize().getHeight()/Grid.HEIGHT);
			cellW = Math.min(cellW, cellH);
			cellH = Math.min(cellW, cellH);
			break;
		}
		Dimension dim = new Dimension(128*cellW, 128*cellH);
		grid.setSize(dim);
		grid.setPreferredSize(dim);
		grid.repaint();
		if (grid.getSize().getWidth()<scrollPane.getSize().getWidth()
			||
			grid.getSize().getHeight()<scrollPane.getSize().getHeight())
			scrollPane.setPreferredSize(dim);
		
		this.pack();
		this.setLocation(new Point((WIDTH-this.getWidth())/2, 0));
	}
	
	public void paintComponent(Graphics g)
	{
		System.out.println("PAINT RATIO("+cellW+","+cellH+")");
		if (grid.getSize().width < scrollPane.getSize().width)
		{
			scrollPane.setPreferredSize(new Dimension(grid.getSize().width, grid.getSize().height));
			System.out.println("resized!!!");
		}
		else
			scrollPane.setPreferredSize(new Dimension((int)this.getSize().getWidth(),(int)this.getSize().getHeight()));
		grid.repaint();
	}

	@Override
	public void windowActivated(WindowEvent paramWindowEvent) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosed(WindowEvent paramWindowEvent) {
		System.out.println("FINESTRA CHIUSA");
		
	}

	@Override
	public void windowClosing(WindowEvent paramWindowEvent) {
		System.out.println("FINESTRA IN CHIUSURA");
		
	}

	@Override
	public void windowDeactivated(WindowEvent paramWindowEvent) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowDeiconified(WindowEvent paramWindowEvent) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowIconified(WindowEvent paramWindowEvent) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowOpened(WindowEvent paramWindowEvent) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowGainedFocus(WindowEvent paramWindowEvent) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowLostFocus(WindowEvent paramWindowEvent) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowStateChanged(WindowEvent paramWindowEvent) {
		int gridX = grid.getWidth();
		int gridY = grid.getHeight();
		
		int minSize = Math.min(gridX, gridY);
		
		if (this.getWidth()>minSize || this.getHeight()>minSize)
		{
			this.pack();
			this.setLocation(new Point((WIDTH-this.getWidth())/2,0));
		}
		else
		{
			if(minSize<WIDTH && minSize<HEIGHT)
				scrollPane.setPreferredSize(new Dimension(minSize, minSize));
			else
			{
				minSize = Math.min(WIDTH, HEIGHT);
				scrollPane.setPreferredSize(new Dimension(minSize, minSize));
			}
					
			this.pack();
			this.setLocation(new Point((WIDTH-this.getWidth())/2,0));
		}
		
	}
}
